<template>
  <div class="doc-box">
    <h3>{{ title }}</h3>
    <div class="preview">
      <div class="content" ref="content">
        <slot name="preview"></slot>
      </div>

      <pre v-show="show">
        <slot name="code"></slot>
      </pre>
      <span @click="onPreview">{{ show ? '隐藏代码' : '查看代码' }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DocBox',
  props: {
    title: String
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    onPreview () {
      this.show = !this.show
      // if (this.code) {
      //   this.code = ''
      // } else {
      //   this.code = this.$refs['content'].innerHTML
      //   this.code = this.code.replace(/data-v-\w+=""/g, "")
      // }
    }
  }
}
</script>

<style scoped lang="scss">
  h3 {
    margin: 10px;
    font-weight: 100;
    font-size: 26px;
  }
  .doc-box {
  }
  .preview {
    border: solid 1px #eee;
    span {
      margin: 5px;
      display: block;
      text-align: right;
      color: #007D71;
      font-size: 14px;
      cursor: pointer;
      &:hover {
        color: #399EE4;
      }
    }
    .content {
      background-color: #fff;
      padding: 25px;
    }
    pre {
      display: block;
      padding: 10px 25px;
      color: #3182bd;
      background: #fafafa;
      code {
        display: block;
      }
    }
  }
</style>
